package com.somsoft.handjc.util;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import com.somsoft.handjc.activity.jiancha.xzsp.XzspJcMainActivity;
import com.somsoft.handjc.activity.query.xzsp.XzspQueryMainActivity;
import com.somsoft.handjc.activity.tongji.xzsp.XzspTongJiMainActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;

public class Utils {

	/**
	 * 完全退出程序
	 * android.os.Process.killProcess(android.os.Process.myPid());
       System.exit(0);
	 */
	public static String SERVER_IP = "http://192.168.1.8:8080";
	public static String REMBER_ME = "true";
	public static String USERNAME = "";
	public static String PASSWORD = "";
	public static String LOGIN_USER = "";
	public static int USER_LEVEL = -1;
	public static String sessionId = "";
	private static ProgressDialog dialog;
	
	//params.add(new BasicNameValuePair("name","this is post"));   
	public synchronized static String getJsonData(String httpUrl,List<NameValuePair> params) throws Exception{
		String str = "";
		HttpPost request = new HttpPost(httpUrl);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		try {
			HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
			request.setEntity(entity);
			//请求超时 
			httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);  
			//读取超时
			httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
			HttpResponse response = httpClient.execute(request);
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				str = EntityUtils.toString(response.getEntity(),"UTF-8");
			}
		} catch (Exception e) {
			throw new Exception("连接服务器超时！");
		} 
		return str;
	}
	
	public static void openLoading(Context c){
		dialog = new ProgressDialog(c); 
		//设置进度条风格，风格为圆形，旋转的 
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
		//设置ProgressDialog 标题 
		dialog.setTitle("请稍候"); 
		//设置ProgressDialog 提示信息 
		dialog.setMessage("数据加载中......"); 
		//设置ProgressDialog 标题图标 
		dialog.setIcon(android.R.drawable.ic_dialog_map); 
		//设置ProgressDialog 的一个Button 
		dialog.setButton("关闭", new ProgressDialog.OnClickListener(){ 
		    @Override 
		    public void onClick(DialogInterface dialog, int which) { 
		         
		    } 
		}); 
		//设置ProgressDialog 的进度条是否不明确 
		dialog.setIndeterminate(false); 
		//设置ProgressDialog 是否可以按退回按键取消 
		dialog.setCancelable(true); 
		//显示 
		dialog.show(); 
	}
	
	public static void closeLoading(){
		if(dialog!=null && dialog.isShowing()){
			dialog.cancel();
		}
	}
	
	/**
	 * 检测访问权限
	 * @param level 1:无限制;2:行政审批;3:公共资源、重大项目
	 * @return
	 */
	@SuppressWarnings("all")
	public static boolean isPass(int level,Class clazz){
		Class[] xzsp = {XzspJcMainActivity.class,XzspQueryMainActivity.class,XzspTongJiMainActivity.class};
		
		if(level == 1){
			return true;
		}else if(level == 2){
			for(Class c : xzsp){
				if(c.getName().equals(clazz.getName())){
					return true;
				}
			}
			return false;
		}else{
			return false;
		}
	}
}
